package org.dxy.trigger.listener.workorder;

import com.google.common.eventbus.Subscribe;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dxy.domain.event.workorder.WorkOrderDeletedEvent;
import org.dxy.infrastructure.persistent.service.WorkOrderViewService;
import org.dxy.trigger.listener.EventListener;
import org.springframework.stereotype.Component;

/**
 * @description: 监听工单删除事件，并从读模型中删除工单记录
 * @author: dxy
 * @createTime: 2025/8/4 22:30
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class WorkOrderDeletedEventListener implements EventListener<WorkOrderDeletedEvent> {

    private final WorkOrderViewService workOrderViewService;

    /**
     * 处理工单删除事件
     * @param event 工单删除事件，包含工单ID等信息
     */
    @Override
    @Subscribe
    public void onEvent(WorkOrderDeletedEvent event) {
        log.info("接收到工单删除事件, event：{}", event.toString());
        
        boolean deleted = workOrderViewService.removeById(event.workOrderId());
        if (deleted) {
            log.info("工单读模型删除成功，工单ID={}", event.workOrderId());
        } else {
            log.warn("工单读模型删除失败，工单ID={}", event.workOrderId());
        }
    }
}